<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible">
    <el-form :model="form">
      <el-form-item :label="type==1?'积分':'能量'" :label-width="formLabelWidth">
        <el-input v-model="form.amout" :placeholder="placeholderTitle"></el-input>
      </el-form-item>
      <el-form-item label="类型" :label-width="formLabelWidth">
        <el-radio-group v-model="radio">
          <el-radio :label="0">充值</el-radio>
          <el-radio :label="1">扣除</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="mit">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import {energyADD,energySub,pointsADD,pointsSub} from "@/api/user/user"
export default {
  data() {
    return{
      title:'积分操作',
      dialogFormVisible:false,
      formLabelWidth: '120px',
      form: {
        amout: '',
        userid:'',
      },
      radio:0,
      type:1,//1是积分，2是能量
      placeholderTitle:''
    }
  },
  methods:{
    async onSubmit(){
      // 积分操作
      if(this.type==1){
        if(!this.form.amout){
          this.$message({
            message: '请输入积分',
            type: 'warning'
          });
          return;
        }
        // 等于0充值
        if(this.radio==0){
          await pointsADD(this.form)
          this.dialogFormVisible=false
          this.$message({
            type: 'success',
            message: '积分充值成功'
          });
        }
        // 等于1扣除
        if(this.radio==1){
          await pointsSub(this.form)
          this.dialogFormVisible=false
          this.$message({
            type: 'success',
            message: '积分扣除成功'
          });
        }
      }
      // 能量操作
      if(this.type==2){
        if(!this.form.amout){
          this.$message({
            message: '请输入能量',
            type: 'warning'
          });
          return;
        }
        // 等于0充值
        if(this.radio==0){
          await energyADD(this.form)
          this.dialogFormVisible=false
          this.$message({
            type: 'success',
            message: '能量充值成功'
          });
        }
        // 等于1扣除
        if(this.radio==1){
          await energySub(this.form)
          this.dialogFormVisible=false
          this.$message({
            type: 'success',
            message: '能量扣除成功'
          });

        }
      }
      this.setForm()
    },
    setForm(){
      this.form.amout=undefined
      this.form.userid=undefined
      this.radio = 0
    },
    open(userid,type){
      this.type=type;
      if(type==1){
        this.title = '积分操作'
        this.placeholderTitle = '请输入积分'
      }
      if(type==2){
        this.title = '能量操作'
        this.placeholderTitle = '请输入能量'
      }
      this.form.userid = userid
      this.dialogFormVisible = true
    },
    mit(){
      this.dialogFormVisible = false
      this.setForm()
    }
  }
}
</script>
<style scoped lang="scss">

</style>
